﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace PrintBarcode.BUS
{
   
    class TStampAll
    {
        UTLDB.UTLDB connect = new UTLDB.UTLDB();
        DataTable data = new DataTable();
        DataTable dataSQL= new DataTable();
        public TStampAll() {
            DataColumn[] dc = new DataColumn[]{ 
                                new DataColumn("T11", Type.GetType("System.String")),
                                new DataColumn("T12", Type.GetType("System.String")),
                                new DataColumn("T21", Type.GetType("System.String")),
                                new DataColumn("T22", Type.GetType("System.String")),
                                new DataColumn("T31", Type.GetType("System.String")),
                                new DataColumn("T32", Type.GetType("System.String")),
                                new DataColumn("T41", Type.GetType("System.String")),
                                new DataColumn("T42", Type.GetType("System.String")),
                                new DataColumn("T51", Type.GetType("System.String")),
                                new DataColumn("T52", Type.GetType("System.String")),
                                new DataColumn("Barcode", Type.GetType("System.String")),
                                new DataColumn("lable", Type.GetType("System.String"))

            };
            data.Columns.AddRange(dc);

        }
        public DataRow Get_DoThiet(int i) {
             DataRow  r = data.NewRow();
             r["T11"] = "TL:";
             r["T51"] = "C:";
                if (dataSQL.Rows[i]["UnitID"].ToString() == "1")
                {
                    r["T12"] = dataSQL.Rows[i]["WSheed"].ToString() + "Gam";
                    r["T52"] = String.Format("{0:0.00}", float.Parse(dataSQL.Rows[i]["PriceShell"].ToString()) + float.Parse(dataSQL.Rows[i]["giahattam"].ToString()) + float.Parse(dataSQL.Rows[i]["giahatlon"].ToString()) + float.Parse(dataSQL.Rows[i]["TienCong"].ToString()));
                }
                else {
                    r["T12"] = FormatUnit(dataSQL.Rows[i]["WSheed"].ToString());
                    r["T52"] = dataSQL.Rows[i]["TienCong"].ToString();
            
                }
                r["T21"] = FormatUnit(dataSQL.Rows[i]["hatlon"].ToString());
                r["T22"] = dataSQL.Rows[i]["giahatlon"].ToString();
                r["T31"] = dataSQL.Rows[i]["hattam"].ToString();
                r["T32"] = dataSQL.Rows[i]["giahattam"].ToString();
                r["T41"] = "V";
                r["T42"] = String.Format("{0:0.00}", float.Parse(dataSQL.Rows[i]["PriceShell"].ToString()) + float.Parse(dataSQL.Rows[i]["giahattam"].ToString()));
                r["BarCode"] = "*" + dataSQL.Rows[i]["Barcode"].ToString() + "*";
                r["Lable"] = dataSQL.Rows[i]["Barcode"].ToString();
               // r["NameUnit"] = dt.Rows[i]["NameUnit"].ToString();

                return r;
        
        }
        public DataRow Get_DoThuong(int i)
        {
            DataRow r = data.NewRow();
            r["T11"] = "TL:";
           // r["T51"] = "C:";
            if (dataSQL.Rows[i]["UnitID"].ToString() == "1")
            {
                r["T12"] = dataSQL.Rows[i]["WSheed"].ToString() + "Gam";
             //   r["T52"] = String.Format("{0:0.00}", float.Parse(dataSQL.Rows[i]["PriceShell"].ToString()) + float.Parse(dataSQL.Rows[i]["giahattam"].ToString()) + float.Parse(dataSQL.Rows[i]["giahatlon"].ToString()) + float.Parse(dataSQL.Rows[i]["TienCong"].ToString()));
            }
            else
            {
                r["T12"] = FormatUnit(dataSQL.Rows[i]["WSheed"].ToString());
               // r["T52"] = dataSQL.Rows[i]["TienCong"].ToString();

            }
            r["T21"] = "H:" + dataSQL.Rows[i]["qualitytam"].ToString();
            r["T22"] = dataSQL.Rows[i]["giahatlon"].ToString();
           // r["T31"] = dataSQL.Rows[i]["hattam"].ToString();
           // r["T32"] = dataSQL.Rows[i]["giahattam"].ToString();
            r["T31"] = dataSQL.Rows[i]["Source"].ToString();
           // r["T42"] = String.Format("{0:0.00}", float.Parse(dataSQL.Rows[i]["PriceShell"].ToString()) + float.Parse(dataSQL.Rows[i]["giahattam"].ToString()));
            r["BarCode"] = "*" + dataSQL.Rows[i]["Barcode"].ToString() + "*";
            r["Lable"] = dataSQL.Rows[i]["Barcode"].ToString();
            // r["NameUnit"] = dt.Rows[i]["NameUnit"].ToString();

            return r;

        }
        public DataRow Get_Vang18k(int i)
        {
            DataRow r = data.NewRow();
            r["T11"] = "TL:";
            // r["T51"] = "C:";
            if (dataSQL.Rows[i]["UnitID"].ToString() == "1")
            {
                r["T12"] = dataSQL.Rows[i]["WSheed"].ToString() + "Gam";
                //   r["T52"] = String.Format("{0:0.00}", float.Parse(dataSQL.Rows[i]["PriceShell"].ToString()) + float.Parse(dataSQL.Rows[i]["giahattam"].ToString()) + float.Parse(dataSQL.Rows[i]["giahatlon"].ToString()) + float.Parse(dataSQL.Rows[i]["TienCong"].ToString()));
            }
            else
            {
                r["T12"] = FormatUnit(dataSQL.Rows[i]["WSheed"].ToString());
                // r["T52"] = dataSQL.Rows[i]["TienCong"].ToString();

            }
            r["T21"] = "H:" + dataSQL.Rows[i]["qualitytam"].ToString();
            r["T22"] = dataSQL.Rows[i]["giahatlon"].ToString();
            r["T31"] = "V:" + dataSQL.Rows[i]["GoldType"].ToString();
           //  r["T32"] = dataSQL.Rows[i]["GoldType"].ToString();
            r["T41"] = "C";
            r["T42"] = String.Format("{0:0.00}", float.Parse(dataSQL.Rows[i]["TienCong"].ToString()));
            r["BarCode"] = "*" + dataSQL.Rows[i]["Barcode"].ToString() + "*";
            r["Lable"] = dataSQL.Rows[i]["Barcode"].ToString();
            // r["NameUnit"] = dt.Rows[i]["NameUnit"].ToString();

            return r;

        }
        public DataTable GetStamp()
        {
            DataTable data = connect.GetDataByStoredProcedure("sp_mavach");
            return data;
        }

        public string FormatUnit(string str)
        {

            string result = "";
            string[] temp = str.Trim().Split('.');
            int len = temp[0].Length;
            switch (len)
            {
                case 1:
                    result = temp[0] + "L";
                    break;
                case 2:
                    result = temp[0].Substring(0, 1) + "P" + temp[0].Substring(1, 1) + "L";
                    break;
                case 3:
                    result = temp[0].Substring(0, 1) + "C" + temp[0].Substring(1, 1) + "P" + temp[0].Substring(2, 1) + "L";
                    break;
                case 4:
                    result = temp[0].Substring(0, 1) + "L" + temp[0].Substring(1, 1) + "C" + temp[0].Substring(2, 1) + "P" + temp[0].Substring(3, 1) + "L";
                    break;
            }
            if (temp.Length == 2)
                result += temp[1];
            return result;
        }
        public DataTable SetData()
        {
            DataRow r;
            int i = 0;
            dataSQL = GetStamp();

            for (i = 0; i < dataSQL.Rows.Count; i++)
            {
                if (dataSQL.Rows[i]["IDType"].ToString()=="2")
                 r = Get_DoThuong(i);
                else if (dataSQL.Rows[i]["IDType"].ToString() == "5")
                 r=  Get_Vang18k(i);
                else
                    r = Get_DoThiet(i);
                data.Rows.Add(r);
            }
            return data;
        }
   
    
    }

}
